#include "DeviceLibrary.h"
#include <iostream>

// Device 类实现
Device::Device(std::string id, float voltage, float current, int state)
    : deviceId(id), voltage(voltage), current(current), state(state) {}

Device::~Device() {}

// RegularDevice 类实现
RegularDevice::RegularDevice(std::string id, float v, float c, int s, int type)
    : Device(id, v, c, s), regularType(type) {}


RegularDevice::RegularDevice(const RegularDevice& other)
    : Device(other.deviceId, other.voltage, other.current, other.state), regularType(other.regularType) {
    std::cout << "[RegularDevice] Copy constructor called.\n";
}

RegularDevice& RegularDevice::operator=(const RegularDevice& other) {
    if (this != &other) {
        deviceId = other.deviceId;
        voltage = other.voltage;
        current = other.current;
        state = other.state;
        regularType = other.regularType;
        std::cout << "[RegularDevice] Copy assignment operator called.\n";
    }
    return *this;
}


RegularDevice::~RegularDevice() {
    std::cout << "[RegularDevice] Destructor called.\n";
}

void RegularDevice::printAll() const {
    std::cout << "=== Regular Device ==="
              << "\nID: " << deviceId
              << "\nVoltage: " << voltage << "V"
              << "\nCurrent: " << current << "A"
              << "\nState: " << state
              << "\nType: " << regularType << "\n\n";
}

// SmartDevice 类实现
SmartDevice::SmartDevice(std::string id, float v, float c, int s, int smartType, int funcStatus)
    : Device(id, v, c, s), smartType(smartType), functionStatus(funcStatus) {}

SmartDevice::SmartDevice(const SmartDevice& other)
    : Device(other.deviceId, other.voltage, other.current, other.state),
      smartType(other.smartType), functionStatus(other.functionStatus) {
    std::cout << "[SmartDevice] Copy constructor called.\n";
}

SmartDevice& SmartDevice::operator=(const SmartDevice& other) {
    if (this != &other) {
        deviceId = other.deviceId;
        voltage = other.voltage;
        current = other.current;
        state = other.state;
        smartType = other.smartType;
        functionStatus = other.functionStatus;
        std::cout << "[SmartDevice] Copy assignment operator called.\n";
    }
    return *this;
}

SmartDevice::~SmartDevice() {
    std::cout << "[SmartDevice] Destructor called.\n";
}

void SmartDevice::printAll() const {
    std::cout << "=== Smart Device ==="
              << "\nID: " << deviceId
              << "\nVoltage: " << voltage << "V"
              << "\nCurrent: " << current << "A"
              << "\nState: " << state
              << "\nType: " << smartType
              << "\nFunction Status: " << functionStatus << "\n\n";
}

void SmartDevice::autoAdjustVoltage(float newVoltage) {
    voltage = newVoltage;
    std::cout << "[SmartDevice] Adjusted voltage to " << newVoltage << "V\n";
}

// DeviceControllerInterface 的实现
class DeviceController : public DeviceControllerInterface {
public:
    void control(Device& device) const override {
        device.printAll();
    }

    // void adjustVoltage(Device& device, float newVoltage) const override {
    //     // 动态检查是否为 SmartDevice
    //     SmartDevice* smartDevice = dynamic_cast<SmartDevice*>(&device);
    //     if (smartDevice) {
    //         smartDevice->autoAdjustVoltage(newVoltage);
    //     } else {
    //         std::cout << "[DeviceController] Voltage adjustment not supported for this device.\n";
    //     }
    // }
};

DeviceControllerInterface::~DeviceControllerInterface() {}

std::shared_ptr<DeviceControllerInterface> createDeviceController() {
    return std::make_shared<DeviceController>();
}